/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClasificacionDePizzasCannyEdgeDetectorBox.java
 *
 * Created on 03-ene-2010, 3:06:38
 */
package clasificaciondepizzas;

import clasificaciondepizzas.models.BaseModel;
import com.sun.media.jai.widget.DisplayJAI;
import javax.media.jai.PlanarImage;
import javax.swing.JOptionPane;
import clasificaciondepizzas.operations.ImageOperations;
import clasificaciondepizzas.operations.ValueBaseOperations;

/**
 *
 * @author Manuel
 */
public class ClasificacionDePizzasCannyEdgeDetectorBox extends javax.swing.JDialog {

    /** Creates new form ClasificacionDePizzasCannyEdgeDetectorBox */
    public ClasificacionDePizzasCannyEdgeDetectorBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        edgeImageScrollPane = new javax.swing.JScrollPane();
        acceptButton = new javax.swing.JToggleButton();
        cancelButton = new javax.swing.JToggleButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(clasificaciondepizzas.ClasificacionDePizzasApp.class).getContext().getResourceMap(ClasificacionDePizzasCannyEdgeDetectorBox.class);
        setTitle(resourceMap.getString("cannyEdgeDetectorBox.title")); // NOI18N
        setModal(true);
        setName("cannyEdgeDetectorBox"); // NOI18N
        setResizable(false);

        edgeImageScrollPane.setViewportBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("edgeImageScrollPane.viewportBorder.title"))); // NOI18N
        edgeImageScrollPane.setName("edgeImageScrollPane"); // NOI18N

        acceptButton.setText(resourceMap.getString("acceptButton.text")); // NOI18N
        acceptButton.setName("acceptButton"); // NOI18N
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(edgeImageScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(acceptButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {acceptButton, cancelButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(acceptButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addComponent(edgeImageScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        image = ImageOperations.cannyEdge(image);
        ValueBaseOperations vbo = new ValueBaseOperations(image);

        bm.setAreaRatio(vbo.areaRatio());
        bm.setAspectRatio(vbo.aspectRatio());
        bm.setEccentricity(vbo.eccentricity());
        bm.setRoundness(vbo.roudness());
        bm.setModified();
        
        setVisible(false);
        dispose();
    }//GEN-LAST:event_acceptButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton acceptButton;
    private javax.swing.JToggleButton cancelButton;
    private javax.swing.JScrollPane edgeImageScrollPane;
    // End of variables declaration//GEN-END:variables
    protected PlanarImage image;
    protected PlanarImage thumbnail;
    private BaseModel bm;

    void setImage(PlanarImage image) {
        this.image = image;
        thumbnail = this.image;
        PlanarImage previewImage; //previsualización en miniatura con el filtro aplicado

        // si la imagen supera el tamaño del panel con desplazamiento se escala a un tamaño menor
        if (image.getWidth() > edgeImageScrollPane.getWidth() || image.getHeight() > edgeImageScrollPane.getHeight()) {
            // si la imagen supera la anchura del panel con desplazamiento se escala a una anchura menor
            if (image.getWidth() > image.getHeight()) {
                float ratio = (float) edgeImageScrollPane.getWidth() / (float) image.getWidth();
                thumbnail = ImageOperations.scale(image, ratio, ratio);
            } // si la imagen supera la altura del panel con desplazamiento se escala a una altura menor
            else {
                float ratio = (float) edgeImageScrollPane.getHeight() / (float) image.getHeight();
                thumbnail = ImageOperations.scale(image, ratio, ratio);
            }
        }

        try {
            // previsualización en miniatura del detector de bordes
            previewImage = ImageOperations.cannyEdge(thumbnail);
            DisplayJAI display = new DisplayJAI(previewImage);
            edgeImageScrollPane.setViewportView(display);
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(this,
                    "No es posible aplicar el detector de bordes.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (RuntimeException e) {
            JOptionPane.showMessageDialog(this,
                    "No es posible previsualizar la imagen.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    void setBaseModel(BaseModel bm) {
        this.bm = bm;
    }
}
